package com.atguigu.daijia.dispatch.xxl.client;


import com.alibaba.fastjson2.JSONObject;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.dispatch.xxl.config.XxlJobClientConfig;
import com.atguigu.daijia.model.entity.dispatch.XxlJobInfo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * https://dandelioncloud.cn/article/details/1598865461087518722
 */
@Slf4j
@Component
public class XxlJobClient {
@Autowired
    private XxlJobClientConfig xxlJobClientConfig;
@Autowired
    private RestTemplate restTemplate;
@SneakyThrows
    public Long addJob(String executorHandler, String param, String corn, String desc) {
    XxlJobInfo xxlJobInfo = new XxlJobInfo();
    xxlJobInfo.setJobGroup(xxlJobClientConfig.getJobGroupId());
    xxlJobInfo.setJobDesc(desc);
    xxlJobInfo.setAuthor("qy");
    xxlJobInfo.setScheduleType("CRON");
    xxlJobInfo.setScheduleConf(corn);
    xxlJobInfo.setGlueType("BEAN");
    xxlJobInfo.setExecutorHandler(executorHandler);
    xxlJobInfo.setExecutorParam(param);
    xxlJobInfo.setMisfireStrategy("FIRST");
    xxlJobInfo.setExecutorRouteStrategy("SERIAL_EXECUTION");
    xxlJobInfo.setExecutorTimeout(0);
    xxlJobInfo.setExecutorFailRetryCount(0);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<XxlJobInfo> request = new HttpEntity<>(xxlJobInfo, headers);
    String url= xxlJobClientConfig.getAddUrl();
    ResponseEntity<JSONObject>response=restTemplate.postForEntity(url,request,JSONObject.class);
    if (response.getStatusCode().value()==200&&response.getBody().getIntValue("code")==200){
        log.info("增加xxl执行任务成功，返回信息：{}",response.getBody().toString());
        //content为任务id
        return  response.getBody().getLong("content");
    }
    log.info("调用xxl增加执行任务失败：{}",response.getBody().toString());
    throw  new GuiguException(ResultCodeEnum.XXL_JOB_ERROR);
}
public  Boolean startJob(Long jobId){
    XxlJobInfo xxlJobInfo = new XxlJobInfo();
    xxlJobInfo.setId(jobId.intValue());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<XxlJobInfo> request = new HttpEntity<>(xxlJobInfo, headers);
    String url=xxlJobClientConfig.getStartJobUrl();
    ResponseEntity<JSONObject>response=restTemplate.postForEntity(url,request,JSONObject.class);
    if (response.getStatusCode().value()==200&&response.getBody().getIntValue("code")==200){
        log.info("启动xxl执行任务成功，返回信息：{}",jobId,response.getBody().toJSONString());
        return true;
    }
    log.info("调用xxl启动执行任务失败：{},返回信息：{}",jobId,response.getBody().toJSONString());
    throw  new GuiguException(ResultCodeEnum.XXL_JOB_ERROR);
}
public  Boolean stopJob(Long jobId){
    XxlJobInfo xxlJobInfo = new XxlJobInfo();
    xxlJobInfo.setId(jobId.intValue());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<XxlJobInfo> request = new HttpEntity<>(xxlJobInfo, headers);
    String url=xxlJobClientConfig.getStopJobUrl();
    ResponseEntity<JSONObject>response=restTemplate.postForEntity(url,request,JSONObject.class);
    if (response.getStatusCode().value()==200&&response.getBody().getIntValue("code")==200){
        log.info("停止xxl执行任务成功，返回信息：{}",jobId,response.getBody().toJSONString());
        return true;
    }
    log.info("调用xxl停止执行任务失败：{},返回信息：{}",jobId,response.getBody().toJSONString());
    throw  new GuiguException(ResultCodeEnum.XXL_JOB_ERROR);
}
public  Boolean removeJob(Long jobId){
    XxlJobInfo xxlJobInfo = new XxlJobInfo();
    xxlJobInfo.setId(jobId.intValue());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<XxlJobInfo> request = new HttpEntity<>(xxlJobInfo, headers);
    String url=xxlJobClientConfig.getRemoveUrl();
    ResponseEntity<JSONObject>response=restTemplate.postForEntity(url,request,JSONObject.class);
    if (response.getStatusCode().value()==200&&response.getBody().getIntValue("code")==200){
        log.info("删除xxl执行任务成功，返回信息：{}",jobId,response.getBody().toJSONString());
        return true;
    }
    log.info("调用xxl删除执行任务失败：{},返回信息：{}",jobId,response.getBody().toJSONString());
    throw  new GuiguException(ResultCodeEnum.XXL_JOB_ERROR);
}
public  Long addAndStartJob(String executorHandler, String param, String corn, String desc) {
    XxlJobInfo xxlJobInfo = new XxlJobInfo();
    xxlJobInfo.setJobGroup(xxlJobClientConfig.getJobGroupId());
    xxlJobInfo.setJobDesc(desc);
    xxlJobInfo.setAuthor("qy");
    xxlJobInfo.setScheduleType("CRON");
    xxlJobInfo.setScheduleConf(corn);
    xxlJobInfo.setGlueType("BEAN");
    xxlJobInfo.setExecutorHandler(executorHandler);
    xxlJobInfo.setExecutorParam(param);
    xxlJobInfo.setMisfireStrategy("FIRST");
    xxlJobInfo.setExecutorRouteStrategy("SERIAL_EXECUTION");
    xxlJobInfo.setMisfireStrategy("FIRE_ONCE_NOW");
    xxlJobInfo.setExecutorTimeout(0);
    xxlJobInfo.setExecutorFailRetryCount(0);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<XxlJobInfo> request = new HttpEntity<>(xxlJobInfo, headers);
    String url=xxlJobClientConfig.getAddAndStartUrl();
    ResponseEntity<JSONObject>response=restTemplate.postForEntity(url,request,JSONObject.class);
    if (response.getStatusCode().value()==200&&response.getBody().getIntValue("code")==200){
        log.info("增加并启动xxl执行任务成功，返回信息：{}",response.getBody().toJSONString());
        //content为任务id
        return  response.getBody().getLong("content");

    }
    log.info("调用xxl增加并启动执行任务失败：{}",response.getBody().toJSONString());
    throw  new GuiguException(ResultCodeEnum.XXL_JOB_ERROR);
}
}

